import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with Intellij IDEA.
 * Description:
 * User:Lenovo
 * Date:2024-12-09
 * Time:14:31
 */
public class MyArrayList<T> {
    private T[] array;
    private int usedsize;
    public MyArrayList(Class<T> clazz,int capacity){
        array = (T[]) Array.newInstance(clazz,capacity);
        array = Arrays.copyOf(array,2*usedsize);

    }
    public void add(T value){
        array[usedsize] = value;
        usedsize++;
    }
    public void add(T value,int index){
        for (int i = usedsize; i >index ; i--) {
            array[i] = array[i-1];
        }
        array[index] = value;
        usedsize++;
    }

    public int getUsedsize() {
        return usedsize;
    }

    @Override
    public String toString() {
        if(array==null){
            return "Null";
        }
        int max = usedsize-1;
        if(max==-1){
            return "[]";
        }
        StringBuilder a = new StringBuilder();
        a.append('[');
        for (int i = 0; i <= max ; i++) {
            a.append(String.valueOf(array[i]));
            if(i==max){
                a.append(']');
                break;
            }
            a.append(" ");
        }
        return String.valueOf(a);
    }

    public void set(int index, T value){
        if(index<0||index>=usedsize){
            throw new error("所定义位置不规范");
        }
        array[index] = value;
    }
    public void remove(int index){
        for (int i = index; i < usedsize-1 ; i++) {
            array[i] = array[i+1];
        }
        usedsize--;
    }
    public void remove(T value){
        for (int i = 0; i < usedsize; i++) {
            if(array[i] == value){
                for (int j = i; j < usedsize-1 ; j++) {
                    array[j] = array[j+1];
                }
                usedsize--;
                return;
            }
        }
        System.out.println("找不到想要删除的值");
    }
    public T get(int index){
        return array[index];
    }
}
